package com.xiaoshuaila.workbench.service;

import com.xiaoshuaila.tool.DateTimeUtil;
import com.xiaoshuaila.tool.SqlSessionUtil;
import com.xiaoshuaila.tool.UUIDUtil;
import com.xiaoshuaila.workbench.dao.*;
import com.xiaoshuaila.workbench.domain.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClueserviceImpl implements Clueservice {
    private ClueDao clueDao=
            SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);
    private ClueActivityRelationDao clueActivityRelationDao=
            SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationDao.class);
    private ClueRemarkDao clueRemarkDao=
            SqlSessionUtil.getSqlSession().getMapper(ClueRemarkDao.class);
    private CustomerDao customerDao=
            SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private CustomerRemarkDao customerRemarkDao=
            SqlSessionUtil.getSqlSession().getMapper(CustomerRemarkDao.class);
    private ContactsRemarkDao contactsRemarkDao=
            SqlSessionUtil.getSqlSession().getMapper(ContactsRemarkDao.class);
    private ContactsActivityRelationDao contactsActivityRelationDao
            =
            SqlSessionUtil.getSqlSession().getMapper(ContactsActivityRelationDao.class);
    private TranDao tranDao=
            SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao=
            SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);
    private ContactsDao contactsDao=
            SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);
    @Override
    public int saveclue(Clue clue) {
        return clueDao.insert(clue);
    }

    @Override
    public Clue detail(String id) {
        return clueDao.detail(id);
    }

    @Override
    public Map pageclue(Map clue) {
        Map map=new HashMap();
        List<Clue>list=clueDao.selectall();
        map.put("list",clueDao.select(clue));
        clue.put("pageSize",null);
        map.put("total",clueDao.select(clue).size());
        return map;
    }

    @Override
    public int deletebund(String id) {
        return clueDao.delete(id);
    }

    @Override
    public boolean insertcar(Map map) {
        String cid= (String) map.get("cid");
        String aid[]= (String[]) map.get("aid");
        ClueActivityRelation clueActivityRelation=null;
        int i=1;
        for (String a:aid
             ) {
            clueActivityRelation=new ClueActivityRelation(UUIDUtil.getUUID(),cid,a);
            i=clueActivityRelationDao.insert(clueActivityRelation);
            if (i==0)
                return false;
        }
        return true;
    }

    @Override
    public boolean conver(String clueid, Tran t, String createby) {
        Boolean flag=true;
        String createtime= DateTimeUtil.getSysTime();
        Clue clue=clueDao.getbyid(clueid);
        String company= clue.getCompany();
        Customer customer=customerDao.getcustomerbyname(company);
        if (customer==null)
        {
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setAddress(clue.getAddress());
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());
            customer.setOwner(clue.getOwner());
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setName(company);
            customer.setDescription(clue.getDescription());
            customer.setCreateTime(createtime);
            customer.setCreateBy(createby);
            customer.setContactSummary(clue.getContactSummary());
            int i=customerDao.save(customer);
            if (i!=1)
            {
                flag=false;
            }
        }
        Contacts contacts=new Contacts();
        contacts.setId(UUIDUtil.getUUID());
        contacts.setSource(clue.getSource());
        contacts.setOwner(clue.getOwner());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setFullname(clue.getFullname());
        contacts.setEmail(clue.getEmail());
        contacts.setDescription(clue.getDescription());
        contacts.setCustomerId(clue.getId());
        contacts.setCreateTime(createtime);
        contacts.setCreateBy(createby);
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setAppellation(clue.getAppellation());
        contacts.setAddress(clue.getAddress());
        int count2=contactsDao.save(contacts);
        if (count2!=1)
            flag=false;
        List<ClueRemark>clueRemarkList=
                clueRemarkDao.getlistbyclueid(clueid);
        for (ClueRemark clueRemark:clueRemarkList)
        {
            CustomerRemark customerRemark=new CustomerRemark();
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setCreateBy(createby);
            customerRemark.setCreateBy(createtime);
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setEditFlag("0");
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            int count3=customerRemarkDao.save(customerRemark);
            if (count3==0)
                flag=false;
            ContactsRemark contactsRemark=new ContactsRemark();
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setCreateBy(createby);
            contactsRemark.setCreateBy(createtime);
            contactsRemark.setContactsId(contacts.getId());
            contactsRemark.setEditFlag("0");
            contactsRemark.setNoteContent(clueRemark.getNoteContent());
            int count4=contactsRemarkDao.save(contactsRemark);
            if (count4==0)
                flag=false;
        }
        List<ClueActivityRelation>clueActivityRelationList=
                clueActivityRelationDao.getlistbyclueid(clueid);
        for (ClueActivityRelation activityRelation:clueActivityRelationList)
        {
            ContactsActivityRelation contactsActivityRelation=
                    new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityRelation.getId());
            contactsActivityRelation.setContactsId(contacts.getId());
            int count5=
                    contactsActivityRelationDao.save(contactsActivityRelation);
            if (count5==0)
                flag=false;
        }
        if (t!=null)
        {
            t.setSource(clue.getSource());
            t.setOwner(clue.getOwner());
            t.setNextContactTime(clue.getNextContactTime());
            t.setDescription(clue.getDescription());
            t.setCustomerId(customer.getId());
            t.setContactSummary(clue.getContactSummary());
            t.setContactsId(contacts.getId());
            int count6=tranDao.save(t);
            if (count6==0)
                flag=false;
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setCreateBy(createby);
            tranHistory.setCreateTime(createtime);
            tranHistory.setExpectedDate(t.getExpectedDate());
            tranHistory.setMoney(t.getMoney());
            tranHistory.setStage(t.getStage());
            tranHistory.setTranId(t.getId());
            int count7=tranHistoryDao.save(tranHistory);
            if (count7==0)
                flag=false;
        }
        for (ClueRemark clueRemark:clueRemarkList){
            int count8=clueRemarkDao.delete(clueRemark);
            if (count8==0)
                flag=false;
        }
        for (ClueActivityRelation clueActivityRelation:clueActivityRelationList
             ) {
            int count9=
                    clueActivityRelationDao.delete(clueActivityRelation);
            if (count9==0)
                flag=false;
        }
        int count10=clueDao.deleteclue(clueid);
        if (count10!=1)
            flag=false;
        return flag;
    }
}
